package gui.newgui.mainwindow;

import gui.newgui.IArCompGuiComponent;
import gui.newgui.IController;
import gui.newgui.archerdbeditor.NewArcherDBEditor;
import gui.newgui.informationpanel.InformationPanel;
import gui.newgui.mainpanel.MainPanel;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import tournamentData.TournamentData;
import core.GuiComponentLabels;

public class MainWindow extends javax.swing.JFrame implements IArCompGuiComponent {
	private JMenuBar menuBar;
	private JMenuItem archerDBEditorMenuItem;
	private JMenuItem importFromCSVMenuItem;
	private JMenuItem closeMenuItem;
	private JMenu importMenuItem;
	private JSplitPane mainSplitPane;
	private JMenuItem newTournamentMenuItem;
	private JMenuItem saveTournamentMenuItem;
	private JMenuItem openTournamentMenuItem;
	private JMenu printMenu;
	private JMenu optionsMenu;
	private JMenu editMenu;
	private JMenu dataMenu;
	private final IController controller;
	private final MenuBarController menuBarController;

	private IArCompGuiComponent mainPanel;
	private IArCompGuiComponent informationPanel;

	public MainWindow(File toOpen) {
		super();
		this.controller = new MainWindowController(this);
		this.menuBarController = new MenuBarController(this);
		this.initGUI();
		TournamentData.getInstance().setOpenedFile(toOpen);
		((MainWindowController) this.controller).loadTournamentData(toOpen);
	}

	private void initGUI() {
		try {
			this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				this.mainPanel = new MainPanel();
				this.informationPanel = new InformationPanel();
				this.mainSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, (Component) this.mainPanel, (Component) this.informationPanel);
				this.mainSplitPane.setOneTouchExpandable(true);
				this.mainSplitPane.setDividerLocation(Integer.MAX_VALUE);
				this.getContentPane().add(this.mainSplitPane, BorderLayout.CENTER);
			}
			{
				this.menuBar = new JMenuBar();
				this.setJMenuBar(this.menuBar);
				{
					this.dataMenu = new JMenu();
					this.menuBar.add(this.dataMenu);
					this.dataMenu.setText(GuiComponentLabels.DATA_MENU);
					{
						this.newTournamentMenuItem = new JMenuItem();
						this.dataMenu.add(this.newTournamentMenuItem);
						this.newTournamentMenuItem.setText(GuiComponentLabels.NEW_TOURNAMENT);
						this.newTournamentMenuItem.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								MainWindow.this.menuBarController.doCreateNewTournament();
							}
						});
					}
					{
						this.openTournamentMenuItem = new JMenuItem();
						this.dataMenu.add(this.openTournamentMenuItem);
						this.openTournamentMenuItem.setText(GuiComponentLabels.OPEN);
						this.openTournamentMenuItem.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								MainWindow.this.menuBarController.doOpen();
							}
						});
					}
					{
						this.saveTournamentMenuItem = new JMenuItem();
						this.dataMenu.add(this.saveTournamentMenuItem);
						this.saveTournamentMenuItem.setText(GuiComponentLabels.SAVE_BUTTON);
						this.saveTournamentMenuItem.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								MainWindow.this.menuBarController.doSave();
							}
						});
					}
					{
						this.importMenuItem = new JMenu();
						this.dataMenu.add(this.importMenuItem);
						this.importMenuItem.setText(GuiComponentLabels.IMPORT);
						{
							this.importFromCSVMenuItem = new JMenuItem();
							this.importMenuItem.add(this.importFromCSVMenuItem);
							this.importFromCSVMenuItem.setText(GuiComponentLabels.FROM_CSV);
						}
					}
					{
						this.closeMenuItem = new JMenuItem();
						this.dataMenu.add(this.closeMenuItem);
						this.closeMenuItem.setText(GuiComponentLabels.CLOSE_MENU_ITEM);
						this.closeMenuItem.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								MainWindow.this.menuBarController.closeWindow();
							}
						});
					}
				}
				{
					this.editMenu = new JMenu();
					this.menuBar.add(this.editMenu);
					this.editMenu.setText(GuiComponentLabels.EDIT_MENU);
					{
						this.archerDBEditorMenuItem = new JMenuItem();
						this.editMenu.add(this.archerDBEditorMenuItem);
						this.archerDBEditorMenuItem.setText(GuiComponentLabels.ARCHERS);
						this.archerDBEditorMenuItem.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								SwingUtilities.invokeLater(new Runnable() {
									public void run() {
										NewArcherDBEditor inst = new NewArcherDBEditor();
										inst.setLocationRelativeTo(null);
										inst.setVisible(true);
									}
								});
							}
						});
					}
				}
				{
					this.optionsMenu = new JMenu();
					this.menuBar.add(this.optionsMenu);
					this.optionsMenu.setText(GuiComponentLabels.OPTIONS_MENU);
				}
				{
					this.printMenu = new JMenu();
					this.menuBar.add(this.printMenu);
					this.printMenu.setText(GuiComponentLabels.PRINT_MENU);
				}
			}
			this.pack();
			this.setSize(890, 456);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	@Override
	public IController getController() {
		return this.controller;
	}

}
